﻿using System;
using System.Collections.Generic;
using Framework.Dependency;
using Interfaces.Criteria;
using Interfaces.Filters;
using Interfaces.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using YQLDataProvider;
using YQLDataProvider.Providers;

namespace DataProviderIntegrationTests.ProvidersTests
{
    [TestClass]
    public class YqlXchangeRateTests
    {
        [ClassInitialize]
        public static void InitTests(TestContext context)
        {
            var module = new YqlDataProviderModule(DependencyManager.Container);
            module.Initialize();
        }

        [TestMethod]
        public void GetDataTest_IsLazyLoadingTrue_OnlyXchangeRateSomeDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (criteria, filter) =>
            {
                criteria.Setup(x => x.IsLazyLoading).Returns(true);
                criteria.Setup(x => x.IsFullLoading).Returns(false);
                criteria.Setup(x => x.Identificators).Returns(new object[] { "EURUSD" });
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            }, when: (provider, criteria, filter) => provider.GetData(criteria, filter), then: (result) =>
            {
                Assert.IsNotNull(result);
                foreach (var xchange in result)
                    Assert.IsNull(xchange.Pair);
            });
        }

        [TestMethod]
        public void GetDataTest_IsDefaultLoading_AllXchangeRateDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (criteria, filter) =>
            {
                criteria.Setup(x => x.IsLazyLoading).Returns(false);
                criteria.Setup(x => x.IsFullLoading).Returns(false);
                criteria.Setup(x => x.Identificators).Returns(new object[] { "EURUSD" });
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            }, when: (provider, criteria, filter) => provider.GetData(criteria, filter), then: (result) =>
            {
                Assert.IsNotNull(result);
                foreach (var xchange in result)
                    Assert.IsNotNull(xchange.Pair);
            });
        }

        private static void YqlIndustryProviderTetsTemplate(Action<Mock<IKeySupportedCriteria>, Mock<IXchangeRateFilter>> given,
                                                 Func<YqlXchangeRateDataProvider, IKeySupportedCriteria, IXchangeRateFilter, IEnumerable<IXchangeRate>> when,
                                                 Action<IEnumerable<IXchangeRate>> then)
        {
            var provider = new YqlXchangeRateDataProvider();
            var keyCriteria = new Mock<IKeySupportedCriteria>();
            var filter = new Mock<IXchangeRateFilter>();
            given(keyCriteria, filter);
            var result = when(provider, keyCriteria.Object, filter.Object);
            then(result);
        }
    }
}
